import collections


class Solution(object):
    def countHighestScoreNodes(self, parents):
        n = len(parents)
        du = [0] * n
        ans = [1] * n
        out = [0] * n
        d = collections.defaultdict(list)
        for index, p in enumerate(parents):
            if p != -1:
                du[p] += 1
                d[p].append(index)
        now = set()
        for i in range(n):
            if not du[i]:
                now.add(i)
        while now:
            next_set = set()
            for node in now:
                p = parents[node]
                if p != -1:
                    ans[p] += ans[node]
                    du[p] -= 1
                    if not du[p]:
                        next_set.add(p)
                base = 1
                for v in d[node]:
                    base *= ans[v]
                if ans[node] < n:
                    base *= (n - ans[node])
                out[node] = base
            now = next_set
        max_val = max(out)
        return out.count(max_val)

data = Solution()
parents = [-1, 2, 0, 2, 0]
print(data.countHighestScoreNodes(parents))
